package com.ihg.hiex.admin.dao.session;

import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.ihg.hiex.admin.entity.session.FinishTask;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.SessionAttribute;
import com.ihg.hiex.entity.session.Status;
import com.ihg.hiex.entity.session.event.SessionEvent;
import com.ihg.hiex.entity.session.meeting.SessionMeeting;
import com.ihg.hiex.entity.session.meeting.Target;
import com.ihg.hiex.entity.session.participator.Type;
import com.ihg.hiex.entity.session.task.SessionTask;
import com.ihg.hiex.entity.user.Administrator;

/**
 *
 * @author Ark
 *
 */
@Dao("hibernate")
public interface SessionDao {
    
    /**
     * 
     * @param status
     * @return
     */
    @Query("SELECT COUNT(*) FROM Session WHERE disabled = false AND sessionStatus = :status")
    public int count(@Variable("status") Status status);
    
    /**
     * 
     * @return
     */
    @Query("SELECT COUNT(*) FROM Session WHERE disabled = false")
    public int count();
    
    /**
     * 
     * @param status
     * @param admin
     * @return
     */
    @Query("SELECT COUNT(*) FROM Session WHERE disabled = false AND sessionStatus = :status AND (sessionHost = :admin OR assessorOwner = :admin OR delegateOwner = :admin)")
    public int count(@Variable("status") Status status, @Variable("admin") Administrator admin);
    
    /**
     * 
     * @param admin
     * @return
     */
    @Query("SELECT COUNT(*) FROM Session WHERE disabled = false AND (sessionHost = :admin OR assessorOwner = :admin OR delegateOwner = :admin)")
    public int count(@Variable("admin") Administrator admin);
    
    /**
     * 
     * @param status
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Session WHERE disabled = false AND sessionStatus = :status ORDER BY startDate DESC")
    public List<Session> list(@Variable("status") Status status, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);

    /**
     * 
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Session WHERE disabled = false")
    public List<Session> list(@Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);

    /**
     * 
     * @param status
     * @return
     */
    @Query("FROM Session WHERE disabled = false AND sessionStatus = :status")
    public List<Session> list(@Variable("status") Status status);
    
    /**
     * 
     * @param status
     * @param admin
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Session WHERE disabled = false AND sessionStatus = :status AND (sessionHost = :admin OR assessorOwner = :admin OR delegateOwner = :admin)")
    public List<Session> list(@Variable("status") Status status, @Variable("admin") Administrator admin, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);
    
    /**
     * 
     * @param admin
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Session WHERE disabled = false AND (sessionHost = :admin OR assessorOwner = :admin OR delegateOwner = :admin)")
    public List<Session> list(@Variable("admin") Administrator admin, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);
    
    /**
     * 
     * @param admin
     * @return
     */
    @Query("FROM Session WHERE disabled = false AND (sessionHost = :admin OR assessorOwner = :admin OR delegateOwner = :admin)")
    public List<Session> list(@Variable("admin") Administrator admin);
    
    /**
     * 
     * @param delegateIds
     * @return
     */
    @Query("SELECT DISTINCT(sessionParticipator.session) FROM SessionParticipator sessionParticipator WHERE sessionParticipator.disabled = false AND sessionParticipator.delegateId in (:delegateIds)")
    public List<Session> list(@Variable("delegateIds") List<Long> delegateIds);
    
    
    /**
     * 
     * @param session
     * @param names
     * @return
     */
    @Query("FROM SessionAttribute WHERE disabled = false AND session = :session AND name in (:names)")
    public List<SessionAttribute> listSessionAttribute(@Variable("session") Session session, @Variable("names") String[] names);
    
    /**
     * 
     * @param sessions
     * @param names
     * @return
     */
    @Query("FROM SessionAttribute WHERE disabled = false AND session in(:sessions) AND name in (:names)")
    public List<SessionAttribute> listSessionAttribute(@Variable("sessions") List<Session> sessions, @Variable("names") String[] names);
    
    /**
     * 
     * @param status
     * @param admin
     * @return
     */
    @Query("FROM Session WHERE disabled = false AND sessionStatus = :status AND (sessionHost = :admin OR assessorOwner = :admin OR delegateOwner = :admin)")
    public List<Session> list(@Variable("status") Status status, @Variable("admin") Administrator admin);

    /**
     * 
     * @param session
     * @param taskName
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND name = :taskName")
    public SessionTask loadSessionTask(@Variable("session") Session session, @Variable("taskName") String taskName);
    
    /**
     * 
     * @param session
     * @param type
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND target = :target")
    public List<SessionTask> listSessionTask(@Variable("session") Session session, @Variable("target") Type type);
    
    /**
     * 
     * @param session
     * @param name
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND name = :name")
    public List<SessionTask> listSessionTask(@Variable("session") Session session, @Variable("name") String name);
    
    /**
     * 
     * @param session
     * @param name
     * @param type
     * @return
     */
    @Query("FROM SessionTask WHERE disabled = false AND session = :session AND name = :name AND target = :type")
    public List<SessionTask> listSessionTask(@Variable("session") Session session, @Variable("name") String name, @Variable("type") Type type);
    
    /**
     * 
     * @param session
     * @return
     */
    @Query("FROM SessionMeeting WHERE disabled = false AND session = :session")
    public List<SessionMeeting> listSessionMeeting(@Variable("session") Session session);
    
    /**
     * 
     * @param session
     * @param name
     * @param target
     * @return
     */
    @Query("FROM SessionMeeting WHERE disabled = false AND session = :session AND name = :name AND target = :target")
    public SessionMeeting loadSessionMeeting(@Variable("session") Session session, @Variable("name") String name, @Variable("target") Target target);
    
    /**
     * 
     * @param session
     * @param type
     * @return
     */
    @Query("FROM SessionEvent WHERE session = :session AND type = :type")
    public SessionEvent loadSessionEvent(@Variable("session") Session session, @Variable("type") com.ihg.hiex.entity.session.event.Type type);
    
    /**
     * 
     * @param session
     * @param parent
     * @param status
     * @return
     */
    @Query("SELECT new com.ihg.hiex.admin.entity.session.FinishTask(task.assessor.id AS userId, COUNT(*) AS finishCount) FROM SessionTask task WHERE task.disabled = false AND task.session = :session AND task.parent = :parent AND task.status = :status GROUP BY task.assessor")
    public List<FinishTask> listAssessorSessionTask(@Variable("session") Session session, @Variable("parent") String parent, @Variable("status") com.ihg.hiex.entity.session.task.Status status);
    
    /**
     * 
     * @param session
     * @param parent
     * @param status
     * @return
     */
    @Query("SELECT new com.ihg.hiex.admin.entity.session.FinishTask(task.delegate.id as userId, COUNT(*) as finishCount) FROM SessionTask task WHERE task.disabled = false AND task.session = :session AND task.parent = :parent AND task.status = :status GROUP BY task.delegate")
    public List<FinishTask> listDelegateSessionTask(@Variable("session") Session session, @Variable("parent") String parent, @Variable("status") com.ihg.hiex.entity.session.task.Status status);
    
    /**
     * 
     * @param session
     * @param name
     * @return
     */
    @Query("FROM SessionAttribute WHERE disabled = false AND session = :session AND name = :name")
    public SessionAttribute loadSessionAttribute(@Variable("session") Session session, @Variable("name") String name);
}